
var roomsUI = '#lstRooms';
var roomsCntUI = '#txtTotalRoom';

var message = function(text) {
	var msgUI = '#txtMessage';
	$(msgUI).empty();
	$(msgUI).append(text);
};

var listRooms = function(respText) {
	var isDbg = miShare(isDebug);
	$(roomsUI).empty();
	var aRowTag = new EJS({
		url : 'rooms.html'
	});
	var cnt = 0;
	$.each(respText, function(index, item) {
		if (isDbg) {
			console.log('item:');
			console.log(typeof item);
			console.log(item);
		}
		cnt++;
		var data = {
			location : item.location,
			name : item.name,
			roomId : item.roomId,
			comments : item.comments
		};
		$(roomsUI).append(aRowTag.render(data));
	});	
	$(roomsCntUI).empty();
	$(roomsCntUI).append(cnt);
};

var listAll = function() {
	var url = '../rest/locator/rooms';
	var data = {};
	var reqMsg = getjax(url, data, function(resp) {
		message('Listed all Rooms');
		listRooms(resp);
	}, function(resp, status) {
		console.log(resp);
		console.log(status);
		alert('Failure to List All Rooms.\nCheck logs for more info.');
	});
	console.log('listAll().Sent:\n' + JSON.stringify(reqMsg));
};

var find = function() {
	var location = $('#txtLocation').val();
	var url = '../rest/locator/rooms/' + location;
	var data = {};
	var reqMsg = getjax(url, data, function(resp) {
		message('Found room in:' + location);
		listRooms(resp);
	}, function(resp, status) {
		console.log(resp);
		console.log(status);
		alert('Failure to Find Room.\nCheck logs for more info.');
	});
	console.log('find().Sent:\n' + JSON.stringify(reqMsg));
};

var delRoom = function() {
	var roomId = $('#txtDelId').val();
	var url = '../rest/locator/rooms/' + roomId;
	var data = {};
	var reqMsg = deljax(url, data, function(resp) {
		console.log('delete successfully called.');
		message('Removed Room.');
		listAll();
	}, function(resp, status) {
		console.log(resp);
		console.log(status);
		alert('Failure to Delete Room.\nCheck logs for more info.');
	});
	console.log('delRoom().Sent:\n' + JSON.stringify(reqMsg));
};

var addRoomJax = function(name, location, roomId, comments, onSuccess, onError) {
	var url = '../rest/locator/rooms';
	var data = {
		name : name,
		location : location,
		roomId : roomId,
		comments : comments
	};
	var reqMsg = posjax(url, data, onSuccess, onError);
	console.log('addRoomJax().Sent:\n' + JSON.stringify(reqMsg));
	return reqMsg;
};

var fillNewRooms = function() {
	addRoomJax('Pangkor', 'khtp', 'khtp001', 'Meeting Room');
	addRoomJax('Tioman', 'khtp', 'khtp002', 'Meeting Room');
	addRoomJax('Sipadan', 'khtp', 'khtp003', 'Meeting Room');
	addRoomJax('Langkawi', 'khtp', 'khtp004', 'Meeting Room');
	addRoomJax('Nilai', 'tpm', 'tpm001', 'Meeting Room');
	addRoomJax('Puchong', 'tpm', 'tpm002', 'Meeting Room');
	addRoomJax('Sri Petaling', 'tpm', 'tpm003', 'Meeting Room');
	addRoomJax('Paloh', 'ipfc', 'ipfc001', 'Canteen');
	addRoomJax('Jelutong', 'ipfc', 'ipfc002', 'Coffee House');
	message('Filled Rooms');
	listAll();
};

var addRoom = function() {
	var name = $('#txtRoomName').val();
	var location = $('#txtRoomLoc').val();
	var roomId = $('#txtRoomId').val();
	var comments = $('#txtRoomComm').val();
	var reqMsg = addRoomJax(name, location, roomId, comments, function(resp) {
		message('Added New Room');
		listAll();
	}, function(resp, status) {
		console.log(resp);
		console.log(status);
		alert('Failure to Add Room\nCheck logs for more info.');
	});	
};
